import java.util.Map;

import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSItem;
import com.speljohan.rsbot.script.wrappers.RSObject;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class BarbarianAgility extends Script {
	int ladder1 = 32015;
	int Log = 2294;
	RSTile failed = new RSTile(2539, 3545);
	int[] FoodID = { 379, 385, 7946, 333 };
	public int eatAT = 10;
	int ClimbingNet = 20211;
	int Ladder2[] = { 3205, 1747 };
	final RSTile RopeTile = new RSTile(2551, 3553);
	int WallClimb = 1948;
	int wall = 2305;
	int height = getPlane();
	public RSTile WALK_TILE = new RSTile(2551, 3554);
	public RSTile[] Path1 = { new RSTile(2532, 3549), new RSTile(2535, 3553) };
	public RSTile[] Path2 = { new RSTile(2545, 3553), new RSTile(2551, 3555) };

	void climbLadder1() {
		if (findObject(ladder1) == null) {
			wait(random(5, 10));
		} else {
			final RSObject Ladder = findObject(ladder1);
			atObject(Ladder, "Climb");
			wait(random(50, 100));
		}
	}

	void CrossingLog() {
		if (findObject(Log) == null) {
			wait(random(10, 20));
		} else {
			setCompass('w');
			final RSObject Logs = findObject(Log);
			atObject(Logs, "Cross");
		}
	}

	void downStairs() {
		if (findObject(Ladder2) == null) {
			wait(random(5, 10));
		} else {
			final RSObject Laddering = findObject(Ladder2);
			atObject(Laddering, "down");
		}
	}

	private void eat(final int id) {
		log("Eating: " + id + ".");
		atInventoryItem(id, "Eat");
		wait(random(1500, 2500));
		return;
	}

	private void eat_check() {
		log("Low HP, checking for something to eat...");
		final RSItem food = getInventoryItemByID(FoodID);
		if (getInventoryCount(food.getID()) >= 1) {
			eat(food.getID());
		} else {
			log("No more food Logging out");
			logout();
			stopAllScripts();
		}
		return;
	}

	boolean Fell() {
		return getMyPlayer().getLocation().getY() < 3543;
	}

	@Override
	public String getAuthor() {
		return "Killa";
	}

	@Override
	public String getName() {
		return "BarbAgility";
	}

	@Override
	public String getScriptCategory() {
		return "Agility";
	}

	@Override
	public String getScriptDescription() {
		String html = "";
		html += "<html>";
		html += "<head>";
		html += "<style type=\"text/css\"> hr {color: white} p {margin-left: 20px}</style>";
		html += "</head>";
		html += "<body>";
		html += "<center>";
		html += "<b><font size=\"6\" color=\"red\">" + getName() + " v"
				+ getVersion() + " c" + "</font></b><br>";
		html += "<font size=\"3\" color=\"red\">Please start in Right infont of the rope. you can tell where the color is different.<br></font>";
		html += "</center>";
		html += "<center><table border=\"0\"><tr><td colspan=\"5\"><center><font size=6><b>== Options and Settings ==</b></font></center></td></tr><tr><td colspan=4><hr></td></tr><tbody><tr><td width=\"135\">";
		html += "<tr><td><b>Eat Settings:</b></td>";
		html += "</td><td><b>Eat at:</b></td><td><center><input type=\"text\" name=\"eatt\" size=2>&nbsp HP(10 by default)</center></td></tr>";
		html += "<tr><td colspan=2><b><font size=\"3\">&nbsp&nbsp BETA VERSION! Enjoy!</font></b></td><td  colspan=\"2\" align=right>"
				+ getVersion() + " &nbsp&nbsp</td></tr></table>";
		html += "</center></body>";
		html += "</html>";
		return html;
	}

	@Override
	public double getVersion() {
		return 1.0;
	}

	@Override
	public int loop() {
		final RSTile myLocation = getMyPlayer().getLocation();
		final int myX = myLocation.getX();
		final int myY = myLocation.getY();
		if (getEnergy() > random(60, 100)) {
			setRun(true);
		}
		setCameraAltitude(true);
		if (getMyPlayer().isMoving()) {
			return random(0, 150);
		}
		final int php = skills.getCurrentSkillLevel(3);
		if (php <= eatAT) {
			eat_check();
		} else {
			wait(random(50, 100));
		}

		if (myX >= 2549 && myX <= 2553 && myY >= 3553 && myY <= 3556) {
			preRopeSwing();
			wait(random(1000, 2000));
			RopeSwing();
		}
		if (myX >= 2548 && myX <= 2554 && myY >= 9949 && myY <= 9954) {
			WalkCheck();
		}
		if (myX >= 2549 && myX <= 2553 && myY >= 3544 && myY <= 3550) {
			CrossingLog();
		}
		if (Fell()) {
			setCompass('N');
			final RSObject Logs = findObject(Log);
			atObject(Logs, "Cross");
			wait(random(2000, 3000));
		} else {
			wait(random(10, 20));
		}
		if (myX >= 2545 && myX <= 2549 && myY >= 3550 && myY <= 3552) {
			walkPathMM(randomizePath(Path2, 2, 2), 20);
		}
		if (myX >= 2533 && myX <= 2537 && myY >= 3545 && myY <= 3547) {
			walkTileMM(failed);
		}
		if (myX >= 2539 && myX <= 2542 && myY >= 3544 && myY <= 3548
				&& findObject(ClimbingNet) != null) {
			NetClimbing();
		}
		if (myX >= 2536 && myX <= 2538 && myY >= 3545 && myY <= 3547) {
			WallWalking();
		}
		if (getMyPlayer().getLocation().equals(new RSTile(2532, 3546))
				&& getPlane() == 1) {
			downStairs();
		}
		if (getMyPlayer().getLocation().equals(new RSTile(2532, 3546))
				&& getPlane() == 0) {
			walkPathMM(randomizePath(Path1, 2, 2), 20);
		}
		if (myX >= 2532 && myX <= 2536 && myY >= 3551 && myY <= 3556) {
			WallClimbing();
		}
		if (myX >= 2541 && myX <= 2542 && myY >= 3552 && myY <= 3554) {
			atObject(getObjectAt(2542, 3553), "Climb");
		}

		if (getMyPlayer().getLocation().equals(new RSTile(2537, 3553))) {
			atObject(getObjectAt(2539, 3553), "Climb");
		}
		if (getMyPlayer().getLocation().equals(new RSTile(2540, 3553))) {
			atObject(getObjectAt(2542, 3553), "Climb");
		}
		if (getMyPlayer().getLocation().equals(new RSTile(2543, 3553))) {
			walkPathMM(randomizePath(Path2, 2, 2), 20);
		}
		return random(5, 10);
	}

	void NetClimbing() {
		setCameraAltitude(true);
		final RSObject Nets = findObject(ClimbingNet);
		atObject(Nets, "Climb");
	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		if (!args.get("eatt").equals("")) {
			eatAT = Integer.parseInt(args.get("eatt"));
		}
		return true;
	}

	void preRopeSwing() {
		atTile(WALK_TILE, "Walk");
	}

	void RopeSwing() {
		setCameraAltitude(false);
		setCompass('N');
		atTile(RopeTile, "Swing");
		setCameraAltitude(true);
	}

	void WalkCheck() {
		if (getMyPlayer().getLocation().equals(new RSTile(2551, 3549))) {
			wait(random(50, 100));
		} else {
			climbLadder1();
		}

	}

	void walknearwall() {
		if (getObjectAt(2535, 3553) == null) {
			wait(random(10, 20));
		} else {
			atObject(getObjectAt(2535, 3553), "Walk");
			wait(random(1000, 2000));
		}
	}

	void WallClimbing() {
		if (findObject(WallClimb) == null) {
			wait(random(5, 10));
		} else {
			wait(random(500, 1000));
			final RSObject LOLZ = findObject(WallClimb);
			atObject(LOLZ, "Climb");
		}
	}

	void WallWalking() {
		if (getPlane() != 1 && getObjectAt(2535, 3547) != null) {
			wait(random(10, 20));
		} else {
			setCompass('N');
			atObject(getObjectAt(2535, 3547), "Walk");
			wait(random(2000, 3000));
		}
	}
}